using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using buildingLib.engineering;

using buildingLib.xcategory;

using Controller;
namespace _dmis.engineering
{
    public partial class FeedBackDlg : _dmis.UIFormBase,ICommand
    {
        
        public FeedBackDlg()
        {
            InitializeComponent();
        }
        buildingLib.engineering.FeedBack m_feedback;
        buildingLib.engineering.MaintainVisit m_visit;
        private void FeedBackDlg_Load(object sender, EventArgs e)
        {
            maintainListBindingSource.DataSource = buildingLib.engineering.MaintainList.GetFeedbackList();
        }
        XConfigs worker;
        XConfigs feedbacker;

        private void maintainListDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex ==0)
            {
                this.maintainBindingSource.DataSource = buildingLib.engineering.Maintain.GetMaintain(((MaintainInfo)this.maintainListBindingSource.Current).ID);

                panel_FeedBack.Visible = false;
            }
        }

        private void btn_operate_Click(object sender, EventArgs e)
        {
            m_feedback = buildingLib.engineering.FeedBack.NewFeedBack(((Maintain)maintainBindingSource.Current));
            feedBackBindingSource.DataSource = m_feedback;
            
            this.cb_visit.Checked = false;
            m_visit = null;
            
            xConfigsBindingSource.DataSource = worker =  XConfigs.GetConfigs(buildingLib.xcategory.ConfigType.Maintainer);
            this.visitorComboBox.Enabled = this.scoreComboBox.Enabled = this.memoTextBox.Enabled = false;

            this.panel_FeedBack.Visible = !this.panel_FeedBack.Visible;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.feedBackBindingSource.EndEdit();
            this.panel_FeedBack.Visible = false;
        }

        private void panel_FeedBack_VisibleChanged(object sender, EventArgs e)
        {
            this.btn_operate.Visible = !panel_FeedBack.Visible;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                this.feedBackBindingSource.EndEdit();
                m_feedback.DoSave();
                this.maintainListBindingSource.DataSource = MaintainList.GetFeedbackList();
                worker.AddConfig(m_feedback.Worker,string.Empty);
                
                if (m_visit != null)
                {
                    this.maintainVisitBindingSource.EndEdit();
                    
                    m_visit.Save();
                    feedbacker.AddConfig(m_visit.Visitor,string.Empty);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.panel_FeedBack.Visible = false;
        }
        

        private void feedBackBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            this.btn_save.Enabled = m_feedback.IsSavable;
        }

        private void maintainBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            this.btn_operate.Enabled = (maintainBindingSource.DataSource!=null);
        }

        private void cb_visit_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cb_visit.Checked)//
            {
                maintainVisitBindingSource.DataSource = m_visit = buildingLib.engineering.MaintainVisit.New(m_feedback.ID);
                this.visitorComboBox.Enabled = this.scoreComboBox.Enabled = this.memoTextBox.Enabled = true;
                xConfigsFeedbackerBS.DataSource = feedbacker = XConfigs.GetConfigs(ConfigType.Maintainer);
            }
            else
            {
                m_visit = null;
            }
        }

        private void feedBackBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            gp_fee.Visible = (m_feedback.Fees != null);
        }
    }
}

